package com.photoeditor.demo.ui.widget.image.watermark;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.photoeditor.R;
import com.photoeditor.demo.model.subscribe.manager.AppVipManager;

/**
 * Created by edward on 17-7-4.
 */

public class WatermarkSelectView extends FrameLayout implements WatermarkLinearLayout.OnWatermarkSelectChangeListener, CustomWatermarkTextDialog.OnInputFinishListener {

    private ImageView mImageView;
    private ImageView mMaskImageView;
    private ImageView mBgImageView;
    private View mVipView;

    private Context mContext;
    private WatermarkManager mWatermarkManager;
    private Watermark mWatermark;
    private boolean mIsSelectedBefore;
    private CustomWatermarkTextDialog mCustomWatermarkTextDialog;

    public WatermarkSelectView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public WatermarkSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public WatermarkSelectView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.watermark_select_view, this, true);
        mImageView = (ImageView) findViewById(R.id.image_view);
        mMaskImageView = (ImageView) findViewById(R.id.image_mask);
        mBgImageView = (ImageView) findViewById(R.id.bg_image_view);
        mVipView = findViewById(R.id.icon_vip);
        mMaskImageView.setVisibility(INVISIBLE);
    }

    public void setImageBitmap(Bitmap bitmap) {
        mImageView.setImageBitmap(bitmap);
    }

    public void setImageResource(int resId) {
        mImageView.setImageResource(resId);
    }

    public void setWatermarkInfo(WatermarkManager watermarkManager, Watermark watermark) {
        mWatermarkManager = watermarkManager;
        mWatermark = watermark;
    }

    @Override
    public void onSelectChange(boolean selected) {
        if (selected) {
            // 跳过第一次初始化设置的select
            if (mIsSelectedBefore && mCustomWatermarkTextDialog == null) {
                mCustomWatermarkTextDialog = new CustomWatermarkTextDialog(mContext);
                mCustomWatermarkTextDialog.setOnInputFinishListener(this);
            }
            if (mIsSelectedBefore
                    && !mCustomWatermarkTextDialog.isShowing()
                    && mWatermark != null
                    && mWatermarkManager != null
                    && mWatermark instanceof CustomTextWatermark) {
                String text = ((CustomTextWatermark) mWatermark).getCustomText();
                if (!TextUtils.equals(text, WatermarkManager.DEFAULT_CUSTOM_TEXT)) {
                    mCustomWatermarkTextDialog.setEditTextString(text);
                }
                mCustomWatermarkTextDialog.show();
            }
            if (mWatermark != null && mWatermark instanceof CustomTextWatermark) {
                mMaskImageView.setVisibility(VISIBLE);
            }
            mBgImageView.setBackgroundResource(R.drawable.watermark_selected_bg);
            mIsSelectedBefore = true;
        } else {
            mBgImageView.setBackground(null);
            mMaskImageView.setVisibility(INVISIBLE);
            mIsSelectedBefore = false;
        }
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            if (mWatermarkManager != null && mWatermark != null) {
                if (mWatermarkManager.isPlaceholderWatermark(mWatermark) && !AppVipManager.isVip()) {
                    mVipView.setVisibility(VISIBLE);
                }
            }
        } else {
            if (mWatermarkManager != null && mWatermark != null) {
                if (mWatermarkManager.isPlaceholderWatermark(mWatermark) && !AppVipManager.isVip()) {
                    mVipView.setVisibility(VISIBLE);
                }
            }
        }
    }

    @Override
    public void onInputFinish(String input) {
        if (mCustomWatermarkTextDialog != null && mCustomWatermarkTextDialog.isShowing()) {
            mCustomWatermarkTextDialog.dismiss();
        }
        if (mWatermark != null && mWatermarkManager != null) {
            CustomTextWatermark textWatermark = (CustomTextWatermark) mWatermark;
            mWatermarkManager.setWatermarkText(textWatermark, input);
        }
    }
}
